﻿using Engine;
using Game;
using System.Xml.Linq;
using Engine.Graphics;


namespace SCIENEW
{
	public class InserterWidget : CanvasWidget
	{

		public SubsystemTerrain m_subsystemTerrain;

		public ComponentInserter m_component;

		public ComponentBlockEntity m_componentBlockEntity;

		public GridPanelWidget m_inventoryGrid;

		public GridPanelWidget m_itemGrid;

		public ButtonWidget m_inMinusButton;

		public ButtonWidget m_inPlusButton;

		protected readonly ButtonWidget m_outPlusButton,
									m_outMinusButton;

		public InserterWidget(IInventory inventory, ComponentInserter componentDriller)
		{
			m_component = componentDriller;
			m_componentBlockEntity = componentDriller.Entity.FindComponent<ComponentBlockEntity>(throwOnError: true);
			m_subsystemTerrain = componentDriller.Project.FindSubsystem<SubsystemTerrain>(throwOnError: true);
			XElement node = ContentManager.Get<XElement>("Widgets/InserterWidget");
			LoadContents(this, node);
			m_inventoryGrid = Children.Find<GridPanelWidget>("InventoryGrid");
			m_itemGrid = Children.Find<GridPanelWidget>("ItemGrid");
			m_inMinusButton = Children.Find<ButtonWidget>("InMinusButton");
			m_inPlusButton = Children.Find<ButtonWidget>("InPlusButton");
			m_outMinusButton = Children.Find<ButtonWidget>("OutMinusButton");
			m_outPlusButton = Children.Find<ButtonWidget>("OutPlusButton");
			int num = 0;
			for (int i = 0; i < m_itemGrid.RowsCount; i++)
			{
				for (int j = 0; j < m_itemGrid.ColumnsCount; j++)
				{
					var inventorySlotWidget = new InventorySlotWidget();
					inventorySlotWidget.AssignInventorySlot(componentDriller, num++);
					m_itemGrid.Children.Add(inventorySlotWidget);
					m_itemGrid.SetWidgetCell(inventorySlotWidget, new Point2(j, i));
				}
			}
			num = 10;
			for (int k = 0; k < m_inventoryGrid.RowsCount; k++)
			{
				for (int l = 0; l < m_inventoryGrid.ColumnsCount; l++)
				{
					var inventorySlotWidget2 = new InventorySlotWidget();
					inventorySlotWidget2.AssignInventorySlot(inventory, num++);
					m_inventoryGrid.Children.Add(inventorySlotWidget2);
					m_inventoryGrid.SetWidgetCell(inventorySlotWidget2, new Point2(l, k));
				}
			}
		}

		public override void Update()
		{
			if (!m_component.IsAddedToProject)
			{
				ParentWidget.Children.Remove(this);
				return;
			}
            Children.Find<LabelWidget>("InCountText").TextAnchor = TextAnchor.HorizontalCenter | TextAnchor.VerticalCenter;
            Children.Find<LabelWidget>("OutCountText").TextAnchor = TextAnchor.HorizontalCenter | TextAnchor.VerticalCenter;
			Children.Find<LabelWidget>("InCountText").Text = string.Format(LanguageControl.GetContentWidgets("InserterWidget", 3), m_component.innum.ToString());
			Children.Find<LabelWidget>("OutCountText").Text = string.Format(LanguageControl.GetContentWidgets("InserterWidget", 4), m_component.outnum.ToString());
			if (m_inPlusButton.IsClicked)
			{
				m_component.innum++;
			}
			if (m_inMinusButton.IsClicked && m_component.innum > 0)
			{
				m_component.innum--;
			}
			if (m_outPlusButton.IsClicked)
			{
				m_component.outnum++;
			}
			if (m_outMinusButton.IsClicked && m_component.outnum > 0)
			{
				m_component.outnum--;
			}
		}
	}
}
